from django.core.paginator import Paginator, EmptyPage, InvalidPage, PageNotAnInteger
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.urls import reverse
from django.views import View

from boxuegu import settings
from courses.models import Course, Lesson

from organization.models import CourseOrg, Teacher


# 公开课页面展示
class Courselist(View):
    def get(self, request):
        user_id = request.user.id
        if user_id is None:
            return redirect(reverse('users:login'))
        coures = Course.objects.get_queryset().order_by('id')
        paginator = Paginator(coures, 2)
        page = 1
        try:
            all_courses = paginator.page(page)

        except PageNotAnInteger:
            # 如果请求的页数不是整数, 返回第一页。
            all_courses = paginator.page(1)
        except InvalidPage:
            # 如果请求的页数不存在, 重定向页面
            return HttpResponse('找不到页面的内容')
        except EmptyPage:
            # 如果请求的页数不在合法的页数范围内，返回结果的最后一页。
            all_courses = paginator.page(paginator.num_pages)

        MEDIA_URL = settings.MEDIA_URL
        hot_courses = []
        sorted_teachers = Course.objects.all().order_by('-students')[:2]
        for value in sorted_teachers:
            hot_courses.append({
                'name': value.name,
                'image': value.image,
                'get_degree_display': value.degree
            })
        return render(request, 'course-list.html',
                      {'all_courses': all_courses, 'MEDIA_URL': MEDIA_URL, 'hot_courses': hot_courses})


# 公开课页面分页展示
class CoursePagelist(View):
    def get(self, request, page_id):
        user_id = request.user.id
        if user_id is None:
            return redirect(reverse('users:login'))
        coures = Course.objects.get_queryset().order_by('id')
        paginator = Paginator(coures, 2)
        try:
            all_courses = paginator.page(page_id)
        except PageNotAnInteger:
            # 如果请求的页数不是整数, 返回第一页。
            all_courses = paginator.page(1)
        except InvalidPage:
            # 如果请求的页数不存在, 重定向页面
            return HttpResponse('找不到页面的内容')
        except EmptyPage:
            # 如果请求的页数不在合法的页数范围内，返回结果的最后一页。
            all_courses = paginator.page(paginator.num_pages)

        MEDIA_URL = settings.MEDIA_URL
        hot_courses = []
        sorted_teachers = Course.objects.all().order_by('-students')[:2]
        for value in sorted_teachers:
            hot_courses.append({
                'name': value.name,
                'image': value.image,
                'get_degree_display': value.degree
            })
        return render(request, 'course-list.html',
                      {'all_courses': all_courses, 'MEDIA_URL': MEDIA_URL, 'hot_courses': hot_courses})


class Coursedetail(View):
    def get(self, request, detail_id):
        courses = Course.objects.get(id=detail_id)
        MEDIA_URL = settings.MEDIA_URL
        count = Lesson.objects.filter(course_id=detail_id).count()
        courseorg = CourseOrg.objects.get(id=courses.course_org_id)
        course_org_count = Course.objects.filter(course_org_id=courseorg.id).count()
        get_teacher_nums = Teacher.objects.filter(org_id=courseorg.id).count()
        course = {
            'name': courses.name,
            'image': courses.image,
            'get_degree_display': courses.degree,
            'learn_times': courses.learn_times,
            'students': courses.students,
            'get_zj_nums': count,
            'category': courses.category,
            'course_org_image': courseorg.image,
            'course_org_name': courseorg.name,
            'course_org_id': courseorg.id,
            'course_org_count': course_org_count,
            'get_teacher_nums': get_teacher_nums,
            'address': courseorg.address
        }
        return render(request, 'course-detail.html', {'course': course, 'MEDIA_URL': MEDIA_URL})
